.TH std::tuple_size 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::tuple_size \- std::tuple_size

.SH Synopsis
   Defined in header <utility>
   template< class T1, class T2 >

   struct tuple_size<std::pair<T1, T2>>               \fI(since C++11)\fP

       : std::integral_constant<std::size_t, 2> { };

   The partial specialization of std::tuple_size for pairs provides a compile-time way
   to obtain the number of elements in a pair, which is always 2, using tuple-like
   syntax.

.SH Member constants

   value    the constant value 2
   \fB[static]\fP \fI(public static member constant)\fP

.SH Member functions

   operator std::size_t converts the object to std::size_t, returns value
                        \fI(public member function)\fP
   operator()           returns value
   \fI(C++14)\fP              \fI(public member function)\fP

.SH Member types

   Type       Definition
   value_type std::size_t
   type       std::integral_constant<std::size_t, value>

.SH Example


// Run this code

 #include <iostream>
 #include <tuple>
 #include <utility>

 template<class T>
 void test([[maybe_unused]]T t)
 {
     [[maybe_unused]]
     int a[std::tuple_size<T>::value]; // can be used at compile time
     std::cout << std::tuple_size<T>::value << '\\n'; // or at run time
 }

 int main()
 {
     test(std::make_tuple(1, 2, 3.14));
     test(std::make_pair(1, 3.14));
 }

.SH Output:

 3
 2

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to              Behavior as published              Correct behavior
   LWG 2313 C++11      specializations for pair were not required to   required
                       be derived from integral_constant

.SH See also

   Structured binding \fI(C++17)\fP             binds the specified names to sub-objects or
                                          tuple elements of the initializer
   tuple_size                             obtains the number of elements of a
   \fI(C++11)\fP                                tuple-like type
                                          \fI(class template)\fP
   std::tuple_size<std::array>            obtains the size of an array
   \fI(C++11)\fP                                \fI(class template specialization)\fP
   std::tuple_size<std::tuple>            obtains the size of tuple at compile time
   \fI(C++11)\fP                                \fI(class template specialization)\fP
   std::tuple_size<std::ranges::subrange> obtains the number of components of a
   (C++20)                                std::ranges::subrange
                                          \fI(class template specialization)\fP
   std::tuple_element<std::pair>          obtains the type of the elements of pair
   \fI(C++11)\fP                                \fI(class template specialization)\fP
